<template>
  <div class="scenicList-wrapper">
    <a-modal
      :visible="formdesigndialog"
      wrapClassName="ant-modal-centered dialog is-portal-color window-max hiddenoverflow"
      width="100%"
      :zIndex="799"
      :footer="null"
      :bodyStyle="{ padding: '0px' }"
      :closable="false"
      :destroyOnClose="true"
    >
      <iframe
        style="display:none"
        ref="buildpublish"
        class="result-wrapper"
        frameborder="0"
        name="buildpublish"
        :src="fromInfo.designType"
      />
      <a-layout>
        <a-layout-header>
          <div data-v-9606ef30 class>
            <div data-v-9606ef30 class="top_logo">
              <a-row>
                <a-col :span="3">
                  <span>
                    <img
                      data-v-9606ef30
                      src="/logo.png"
                      style="width: 40px; height: 40px;"
                    /><span
                      style="font-size: 20px;font-weight: bold; font-family: cursive;margin-left: 10px;padding-top: 5px;"
                      >麒峰软件</span
                    ></span
                  >
                  <svg
                    @click="
                      showleftright = !showleftright
                      showcodeeditor = false
                    "
                    data-v-3836a050
                    t="1492500959545"
                    viewBox="0 0 1024 1024"
                    version="1.1"
                    xmlns="http://www.w3.org/2000/svg"
                    p-id="1691"
                    xmlns:xlink="http://www.w3.org/1999/xlink"
                    width="14"
                    height="14"
                    class="top_ioc"
                  >
                    <path
                      data-v-3836a050
                      d="M966.8023 568.849776 57.196677 568.849776c-31.397081 0-56.850799-25.452695-56.850799-56.850799l0 0c0-31.397081 25.452695-56.849776 56.850799-56.849776l909.605623 0c31.397081 0 56.849776 25.452695 56.849776 56.849776l0 0C1023.653099 543.397081 998.200404 568.849776 966.8023 568.849776z"
                      p-id="1692"
                    />
                    <path
                      data-v-3836a050
                      d="M966.8023 881.527125 57.196677 881.527125c-31.397081 0-56.850799-25.452695-56.850799-56.849776l0 0c0-31.397081 25.452695-56.849776 56.850799-56.849776l909.605623 0c31.397081 0 56.849776 25.452695 56.849776 56.849776l0 0C1023.653099 856.07443 998.200404 881.527125 966.8023 881.527125z"
                      p-id="1693"
                    />
                    <path
                      data-v-3836a050
                      d="M966.8023 256.17345 57.196677 256.17345c-31.397081 0-56.850799-25.452695-56.850799-56.849776l0 0c0-31.397081 25.452695-56.850799 56.850799-56.850799l909.605623 0c31.397081 0 56.849776 25.452695 56.849776 56.850799l0 0C1023.653099 230.720755 998.200404 256.17345 966.8023 256.17345z"
                      p-id="1694"
                    />
                  </svg>
                </a-col>
                <a-col :span="21">
                  <span @click="clickdesignType('pc')">
                    <nx-svg-icon
                      class-name="PC-icon"
                      icon-class="PC"
                      :class="
                        fromInfo.deviceDesignType == 'pc'
                          ? 'designType selectdesignType'
                          : 'designType'
                      "
                    />
                  </span>
                  <span @click="clickdesignType('pad')">
                    <nx-svg-icon
                      class-name="iPad-icon"
                      icon-class="iPad"
                      :class="
                        fromInfo.deviceDesignType == 'pad'
                          ? 'designType selectdesignType'
                          : 'designType'
                      "
                    />
                  </span>
                  <span @click="clickdesignType('mobile')">
                    <nx-svg-icon
                      class-name="phone-icon"
                      icon-class="phone"
                      :class="
                        fromInfo.deviceDesignType == 'mobile'
                          ? 'designType selectdesignType'
                          : 'designType'
                      "
                    />
                  </span>
                  <!-- <el-select
                    v-model="fromInfo.designType"
                    placeholder="请选择"
                    style="    width: 120px;"
                  >
                    <el-option label="element" value="/buildpublish.html">
                    </el-option>
                    <el-option
                      label="Ant Design  Vue"
                      value="/buildpublishAntDesign.html"
                    ></el-option>
                    <el-option
                      label="问卷调查"
                      value="/buildquestionnaire.html"
                    >
                    </el-option>
                    <el-option
                      label="大屏分析"
                      value="/buildscreenanalyse.html"
                    >
                    </el-option>
                    <el-option label="流程表单" value="/buildFlowFrom.html">
                    </el-option>
                  </el-select> -->
                  <!-- <span @click="preview">
                    <nx-svg-icon
                      class-name="run-icon"
                      icon-class="run"
                      class="designType selectdesignType"
                    />运行
                  </span> -->
                  <el-button-group>
                    <el-button
                      @click="previewfun(3)"
                      class="el-icon-video-play"
                    >
                      预览</el-button
                    >
                    <el-button @click="showcode" class="el-icon-code"
                      ><span v-if="showcodeeditor === false">
                        <a-icon type="bug" />
                        源码</span
                      ><span v-else>
                        <nx-svg-icon
                          class-name="design-icon"
                          icon-class="design"
                        />
                        设计</span
                      ></el-button
                    >
                    <el-button @click="saveform()"
                      ><a-icon type="save" /> 保存</el-button
                    >
                    <!-- <el-button class="fa fa-th-list"> 保存为模板</el-button>
                    <el-button class="fa fa-save"> 另存为</el-button>
                    <el-button class="fa fa-refresh">重新加载</el-button>
                    <el-button class="el-icon-s-grid">选择模板</el-button> -->
                    <el-button @click="ViewJson" class="el-icon-view"
                      >查看json</el-button
                    >
                    <el-button @click="selectfilefun" class="el-icon-upload2"
                      >导入</el-button
                    >
                    <el-button @click="exportForm" class="el-icon-download"
                      >导出</el-button
                    >
                    <el-button @click="exportVue" class="el-icon-download"
                      >导出Vue</el-button
                    >
                    <el-button @click="clearForm" class="el-icon-delete"
                      >清空</el-button
                    >
                    <el-button
                      @click="formdesigndialog = false"
                      class="el-icon-close"
                      >关闭</el-button
                    >
                  </el-button-group>
                </a-col>
              </a-row>
            </div>
          </div>
        </a-layout-header>
        <a-layout v-if="showcodeeditor === false">
          <a-layout-sider v-if="showleftright" width="280px">
            <tools :data="widgetForm" @previewfun="previewfun"></tools>
          </a-layout-sider>
          <a-layout-content
            style="border: 1px solid #91d5ff;
    margin-left: 4px;"
          >
            <template v-if="editcontroltype === 0">
              <reportdesign
                :isadapt="fromInfo.isadapt"
                :cwidth="fromInfo.fwidth"
                :cheight="fromInfo.fheight"
                :cbackgroundurl="fromInfo.backgroundurl"
              >
                <div :class="fromInfo.deviceDesignType" slot="content">
                  <div
                    v-if="fromInfo.designType === '/buildquestionnaire.html'"
                  >
                    <h3 id="zhu-se" class="fromtitle">
                      <a-input
                        v-model="fromInfo.formName"
                        placeholder="请输入内容"
                      ></a-input>
                    </h3>
                    <div class="form_desc_content">
                      <a-textarea
                        v-model="fromInfo.formDesc"
                        placeholder="Controlled autosize"
                        :auto-size="{ minRows: 2, maxRows: 5 }"
                      />
                    </div>
                  </div>
                  <el-col
                    v-for="(item, index) in fromInfo.layoutitemlist"
                    :span="item.span"
                    v-bind:key="index"
                  >
                    <widgetForm
                      :data="item.widgetForm"
                      :activePanel.sync="activePanel"
                      :select.sync="widgetFormSelect"
                      :editcontroltype.sync="editcontroltype"
                      :currentcontroldata.sync="currentcontroldata"
                      :basecontroldata.sync="basecontroldata"
                      @showcode="showcode"
                    ></widgetForm>
                  </el-col>
                </div>
              </reportdesign>
            </template>
            <template v-else-if="editcontroltype === 1">
              <a-card
                class="box-card"
                style="position:initial;   border: 1px solid #bfbfbf;"
                :style="{
                  width:
                    dialogData.options.width.indexOf('%') >= 0 ||
                    dialogData.options.width.indexOf('calc') >= 0
                      ? dialogData.options.width
                      : dialogData.options.width + 'px',
                  height:
                    dialogData.options.height.indexOf('%') >= 0 ||
                    dialogData.options.height.indexOf('calc') >= 0
                      ? dialogData.options.height
                      : dialogData.options.height + 'px'
                }"
                @click.native.stop="handleSelectWidget(dialogData)"
              >
                <div
                  slot="cover"
                  class="clearfix"
                  style="height: 40px; padding: 10px;   border: 1px solid #e8e8e8"
                  v-if="dialogData.options.layertype === 'center'"
                >
                  <span>{{ dialogData.options.labelTitle }}</span>
                  <a
                    class="layermend"
                    @click.stop="editcontroltype = 0"
                    style="float: right;"
                    ><i class="el-icon-close" style="font-size: 20px;"></i
                  ></a>
                </div>

                <widgetForm
                  :data="dialogData"
                  :activePanel.sync="activePanel"
                  :select.sync="widgetFormSelect"
                  :editcontroltype.sync="editcontroltype"
                  :currentcontroldata.sync="currentcontroldata"
                  :basecontroldata.sync="basecontroldata"
                  @showcode="showcode"
                ></widgetForm>
                <template
                  slot="actions"
                  class="ant-card-actions"
                  style="    position: absolute;
    padding-left: 10px;
    width: 789px;"
                >
                  <div style="    float: right;">
                    <el-button
                      v-for="but in dialogData.options.buttons"
                      :key="but"
                      @click.stop="dialogbutonclick(but)"
                      @dblclick.native.stop="handleBuildBtnEvent(but)"
                      :type="but.options.butType"
                      style="margin-right:10px"
                      :size="but.options.butsize"
                      :icon="but.options.icon"
                      >{{ but.options.labelTitle }}</el-button
                    >
                  </div>
                </template>
              </a-card>
            </template>
            <template v-else-if="editcontroltype === 2">
              <el-col
                :md="basecontroldata.options.ctrlSpan"
                :lg="basecontroldata.options.ctrlSpan"
                style="padding:0px;border: 1px dashed #eee;"
                class="widget-view"
              >
                <el-row>
                  <el-col
                    class="border-content"
                    :style="{
                      padding: '0px',
                      height:
                        basecontroldata.options.rowSpan !== undefined
                          ? basecontroldata.options.rowSpan * 42 + 'px'
                          : ''
                    }"
                  >
                    <draggable
                      class="widget-form-list widget-grid"
                      style="padding-bottom:50px;border:0px solid #e8e8e8;min-height:38px"
                      v-model="currentcontroldata.list"
                      filter="widget-grid-container"
                      v-bind="{
                        group: 'people1',
                        ghostClass: 'ghost',
                        animation: 150
                      }"
                      @end="handleMoveEnd"
                      @add="handleWidgetColAdd($event, currentcontroldata, 1)"
                    >
                      <WidgetFormItem
                        v-for="(el, i) in currentcontroldata.list"
                        :key="i"
                        :element="el"
                        :select.sync="widgetFormSelect"
                        :index="i"
                        :data="currentcontroldata"
                        @editpartlayout="editpartlayout"
                        @showcode="showcode"
                      ></WidgetFormItem>
                    </draggable>
                    <el-button
                      title="关闭"
                      @click.stop="closeedit(basecontroldata)"
                      class="el-icon-close"
                      style="position: absolute;right: 0px;top: -0px;z-index: 100;padding:1px;"
                      plain
                      type="danger"
                    ></el-button>
                    <el-button
                      title="编辑"
                      @click.stop="handleSelectWidget(basecontroldata)"
                      class="el-icon-edit"
                      style="position: absolute;right: 20px;top: 0px;z-index: 100;    padding: 1px;"
                      plain
                      type="danger"
                    ></el-button>
                  </el-col>
                </el-row>
              </el-col>
            </template>
            <template v-else-if="editcontroltype === 3">
              <div class="navbar edit_navbar">
                <div
                  :class="
                    previewType === 'phone'
                      ? 'exchange_model mobile icon_wj icon_appearset active high_light'
                      : 'exchange_model mobile icon_wj icon_appearset  '
                  "
                  @click="previewfun(3, 'phone')"
                >
                  <i class="el-icon-mobile-phone" style="font-size: 20px;"></i
                  >手机
                </div>
                <div
                  :class="
                    previewType === 'pc'
                      ? 'exchange_model computer icon_wj active icon_appearset'
                      : 'exchange_model computer icon_wj icon_appearset'
                  "
                  data-type="PC"
                  id="pc_icon"
                  @click="previewfun(3, 'pc')"
                >
                  <i class="el-icon-s-platform" style="font-size: 20px;"></i
                  >电脑
                </div>
              </div>
              <div class="preview_iframe">
                <div
                  class="phone"
                  data-type="Phone"
                  v-if="previewType === 'phone'"
                >
                  <iframe
                    ref="PhoneSurvey"
                    name="PhoneSurvey"
                    frameborder="0"
                    :src="'/preview?deviceMode=Mobile'"
                    class="uploadfile"
                    id="mobile_survey"
                    @load="PhoneSurveyiframeLoad"
                  ></iframe>
                </div>
                <div class="computer active" data-type="PC" v-else>
                  <iframe
                    ref="PcSurvey"
                    name="PcSurvey"
                    frameborder="0"
                    scrolling="no"
                    :src="'/preview'"
                    class="uploadfile"
                    id="pc_survey"
                    style="height:calc(100vh - 120px);"
                    @load="PcSurveyiframeLoad"
                  ></iframe>
                </div>
              </div>
            </template>
          </a-layout-content>
          <a-layout-sider width="280px" v-if="showleftright">
            <a-tabs type="card" style="font-size: 12px;" v-model="righttab">
              <a-tab-pane key="1" tab="控件" class="tabright">
                <widgetConfig
                  :data="widgetFormSelect"
                  :currentcontroldata.sync="currentcontroldata"
                  :basecontroldata.sync="basecontroldata"
                  :editcontroltype.sync="editcontroltype"
                  :select.sync="widgetFormSelect"
                  :fromInfo="fromInfo"
                ></widgetConfig>
              </a-tab-pane>
              <a-tab-pane key="2" tab="事件" class="tabright"
                ><eventconfig
                  :controldata="widgetFormSelect"
                  @addevent="showcode"
                ></eventconfig
              ></a-tab-pane>
              <a-tab-pane key="3" tab="表单" class="tabright">
                <formConfig
                  :select.sync="widgetFormSelect"
                  :dialogData.sync="dialogData"
                  :editcontroltype.sync="editcontroltype"
                  :righttab.sync="righttab"
                ></formConfig>
              </a-tab-pane>
            </a-tabs>
          </a-layout-sider>
        </a-layout>
        <a-layout :style="{ display: showcodeeditor ? '' : 'none' }">
          <codeeditor
            ref="codeeditor"
            :formData="formData"
            @formatDocument="formatDocument"
            @addApiCode="addApiCode"
            :h="codeh"
          ></codeeditor>
        </a-layout>
      </a-layout>
      <slot slot="header" />
    </a-modal>
    <input
      type="file"
      class="file"
      style="display:none"
      id="selectfile"
      ref="selectfile"
      @change="importForm()"
    />
    <!-- <zhlDialog
      title="窗体预览"
      ref="apiconfiginfodialog"
      width="1260px"
      class="my-dialog"
    >
      <div id="previewMain">
        <previewpage ref="previewPagemain" :postData="postData"></previewpage>
      </div>
    </zhlDialog> -->
    <zhlDialog
      title="JSON格式查看"
      ref="fromJsonViewdialog"
      width="1260px"
      class="my-dialog"
    >
      <monaco
        ref="monacoedit"
        language="json"
        :codeStr="fromJson"
        @loadInitAfter="loadAfter"
        :height="600"
      ></monaco>
    </zhlDialog>
    <selectApilist
      ref="selectApilistcontrol"
      @select="confirmSelect"
    ></selectApilist>
  </div>
</template>
<script>
/* eslint-disable */
import FileSaver from 'file-saver'
import tools from '@/components/formdesign/tools'
import widgetForm from '@/components/formdesign/widgetForm'
import widgetConfig from '@/components/formdesign/widgetConfig.vue'
import formConfig from '@/components/formdesign/formConfig.vue'
import Draggable from 'vuedraggable'
import WidgetFormItem from '@/components/formdesign/widgetFormItem'
import eventconfig from '@/components/formdesign/eventconfig.vue'
import codeeditor from '@/components/formdesign/codeeditor'
import { saveFromInfo, getFromInfo } from '@/api/tappfrom.js'
import previewpage from '@/views/index.vue'
import monaco from '@/components/monaco/index.vue'
import nxSvgIcon from '@/components/nx-svg-icon/index'
import loadBeautifier from '@/utils/loadBeautifier'
import selectApilist from '@/components/formdesign/selectApilist.vue'
import reportdesign from '@/views/fromdesign/reportdesign/index.vue'
import { apiComm } from '@/api/index.js'
import { defaultfromInfo } from '@/components/formdesign/formconfig.js'
import { cotorlEventConfig } from '@/components/formdesign/eventconfig.js'
import '@/styles/font-awesome.min93e3.css'
let beautifier
export default {
  data() {
    return {
      formdesigndialog: false,
      showleftright: true,
      fromInfo: {},
      widgetForm: {
        list: []
      },
      codeh: 600,
      selectWidget: null,
      activePanel: 'formProperty',
      widgetFormSelect: null,
      editcontroltype: 0, ///等于0 代表编辑表单 1 编辑弹出层，2代表编辑局部模块
      dialogData: null,
      currentcontroldata: null,
      basecontroldata: null,
      formData: { html: '', js: '', css: '', systemcss: '' },
      showcodeeditor: false,
      queryparam: { fromname: '' },
      postData: {},
      scopedSlots: [],
      fromJson: '',
      righttab: '1',
      previewType: 'phone'
    }
  },
  components: {
    tools,
    widgetForm,
    widgetConfig,
    formConfig,
    WidgetFormItem,
    Draggable,
    codeeditor,
    eventconfig,
    previewpage,
    monaco,
    nxSvgIcon,
    selectApilist,
    reportdesign
  },
  methods: {
    handleMoveEnd(evt) {
      console.log('end', evt)
    },
    editpartlayout(docelement) {
      console.log(docelement)
      ///this.currentcontroldata = docelement
    },
    selectfilefun() {
      document.getElementById('selectfile').click()
    },
    ViewJson() {
      this.fromJson = JSON.stringify(this.fromInfo)
      this.$refs.fromJsonViewdialog.visible = true
      if (this.$refs.monacoedit !== undefined) {
        this.$refs.monacoedit.setvalue(beautifier.js(this.fromJson))
      }
      this.$refs.fromJsonViewdialog.$nextTick(() => {
        this.$refs.monacoedit.loadInit()
      })
    },
    importForm() {
      if (document.querySelector('input[type=file]').files.length === 0) {
        return
      }
      let fileSelect = document.querySelector('input[type=file]').files[0] //找到文件上传的元素
      let reader = new FileReader()
      if (typeof FileReader === 'undefined') {
        console.log('您的浏览器不支持FileReader接口')
        return
      }
      const _this = this
      reader.onload = function() {
        const formid = _this.fromInfo.formid
        const fromdata = JSON.parse(reader.result)
        _this.fromInfo = fromdata.fromInfo
        _this.fromInfo.formid = formid
        _this.formData.js = beautifier.js(fromdata.js)
        _this.formData.css = beautifier.js(fromdata.css)
        if (_this.$refs.codeeditor !== undefined) {
          _this.$refs.codeeditor.setValue('js', _this.formData.js)
          _this.$refs.codeeditor.setValue('css', _this.formData.css)
        }
        _this.$forceUpdate()
      }
      reader.readAsText(fileSelect) //注意读取中文的是用这个编码，不是utf-8
      this.$refs.selectfile.value = null
    },
    exportForm() {
      const data = JSON.stringify({
        fromInfo: this.fromInfo,
        js: this.getjscode(),
        css: this.getcssCode()
      })
      const blob = new Blob([data], { type: '' })
      const filename =
        this.fromInfo.formName !== '' ? this.fromInfo.formName : 'from'
      // const textStr = 'aaaaa,bbbbbbb,cccccc'
      FileSaver.saveAs(blob, filename + '.json')
    },
    clearForm() {
      this.fromInfo.layerlist = []
      this.fromInfo.layoutitemlist = JSON.parse(
        JSON.stringify(defaultfromInfo.layoutitemlist)
      )
      this.formData.html = ''
      this.formData.js = ''
      this.formData.css = ''
      this.editcontroltype = 0
      if (this.$refs.codeeditor !== undefined) {
        this.$refs.codeeditor.setValue('js', '')
        this.$refs.codeeditor.setValue('css', '')
        this.$refs.codeeditor.setValue('html', '')
      }
    },
    formatDocument() {
      const jscode = this.$refs.codeeditor.getEditorValue('js')
      this.$refs.codeeditor.setValue('js', beautifier.js(jscode))
    },
    addApiCode() {
      this.$refs.selectApilistcontrol.openform()
    },
    closeedit(data) {
      this.editcontroltype = 0
      this.widgetFormSelect = data
      this.showcodeeditor = false
    },
    handleSelectWidget(data) {
      this.widgetFormSelect = data
    },
    saveform() {
      const buildObject = window.frames.buildpublish.buildpubish(
        this.fromInfo,
        this.getjscode()
      )
      if (buildObject === false) {
        this.$message.error(`编辑源码脚本有错误！请检查！`)
        this.showcodeeditor = false
        return
      }
      this.formData.html = buildObject.htmlcode
      this.formData.js = buildObject.jscode
      this.formData.systemcss = buildObject.csscode
      const fromdatainfo = {
        id: this.fromInfo.formid,
        fromName: this.fromInfo.formName,
        fremarks: this.fromInfo.formDesc,
        designType: this.fromInfo.designType,
        fromTableName: this.fromInfo.fromTableName,
        fromcontent: JSON.stringify(this.formData),
        fromdesignjson: JSON.stringify(this.fromInfo)
      }
      if (this.fromInfo.formName === '') {
        this.$message.error(`表单名称不能为空！`)
        return
      }
      saveFromInfo(fromdatainfo).then(res => {
        this.$message.success(`保存成功！`)
        this.formdesigndialog = false
        if (this.$refs.gridlist != undefined) {
          this.$refs.gridlist.loadData(1)
        }
        if (
          window.frames['editFromTempiframe'] !== undefined &&
          window.frames['editFromTempiframe'].flowdataObj !== undefined &&
          typeof window.frames['editFromTempiframe'].flowdataObj
            .UpdateVariable !== 'undefined'
        ) {
          window.frames['editFromTempiframe'].flowdataObj.UpdateVariable(
            this.fromInfo,
            res.data
          )
        }
      })
    },
    getjscode() {
      if (this.$refs.codeeditor !== undefined) {
        const isloadjscode = this.$refs.codeeditor.getEditorValue('js')
        if (isloadjscode === false) {
          return this.formData.js
        } else {
          return isloadjscode
        }
      } else {
        return this.formData.js
      }
    },
    getcssCode() {
      if (this.$refs.codeeditor !== undefined) {
        return this.$refs.codeeditor.getEditorValue('css')
      } else {
        return this.formData.css
      }
    },
    confirmSelect(source) {
      const ajaxcode = window.frames.buildpublish.buildsource(source)
      this.$refs.codeeditor.insertContent(ajaxcode)
      this.formatDocument()
    },
    loadfromInfo(fromid) {
      getFromInfo(fromid).then(res => {
        this.fromInfo = JSON.parse(res.data.fromdesignjson)
        let fromcontent =
          res.data.fromcontent === ''
            ? { js: '', css: '', html: '', systemcss: '' }
            : JSON.parse(res.data.fromcontent)
        this.formData.js = fromcontent.js
        this.formData.css = fromcontent.css
        this.formData.html = fromcontent.html
        this.fromInfo.formid = res.data.fid
        if (this.fromInfo.designType === undefined) {
          this.fromInfo.designType = '/buildpublish.html'
        }
        if (this.$refs.codeeditor !== undefined) {
          this.$refs.codeeditor.setValue('js', beautifier.js(this.formData.js))
          this.$refs.codeeditor.setValue(
            'css',
            beautifier.css(this.formData.css)
          )
          this.$refs.codeeditor.setValue(
            'html',
            beautifier.html(this.formData.html)
          )
        }
      })
    },
    handleWidgetColAdd($event, row, colIndex) {
      // console.log('handleWidgetColAdd', $event, row, colIndex)
      let newIndex = $event.newIndex
      const oldIndex = $event.oldIndex
      if (newIndex >= row.list.length) {
        newIndex = row.list.length - 1
      }
      const item = $event.item
      // console.log('handleWidgetColAdd newIndex', newIndex)
      // console.log('handleWidgetColAdd oldIndex', oldIndex)
      if (item.className.indexOf('data-grid') >= 0) {
        item.tagName === 'DIV' &&
          this.data.list.splice(oldIndex, 0, row.list[newIndex])

        row.list.splice(newIndex, 1)

        return false
      }

      // console.log('from', item)

      const key =
        Date.parse(new Date()) + '_' + Math.ceil(Math.random() * 99999)

      this.$set(row.list, newIndex, {
        ...row.list[newIndex],
        options: {
          ...Object.assign({}, row.list[newIndex].options),
          remoteFunc: 'func_' + key
        },
        key,
        model: row.list[newIndex].type + '_' + key,
        rules: []
      })
      // console.log('newItem', row.list[newIndex].type)
      const labelId =
        Date.parse(new Date()) / 1000 + '' + Math.ceil(Math.random() * 99999)
      const ctrlId =
        Date.parse(new Date()) / 1000 + '' + Math.ceil(Math.random() * 99999)
      // console.log(labelId + '_' + ctrlId)
      if (row.list[newIndex].options.labelId !== undefined) {
        this.$set(
          row.list[newIndex].options,
          'labelId',
          row.list[newIndex].options.labelId + labelId
        )
      }

      if (row.list[newIndex].options.controlId !== undefined) {
        this.$set(
          row.list[newIndex].options,
          'controlId',
          row.list[newIndex].options.controlId + ctrlId
        )
      }
      if (
        row.list[newIndex].type === 'radio' ||
        row.list[newIndex].type === 'checkbox' ||
        row.list[newIndex].type === 'select'
      ) {
        this.$set(row.list, newIndex, {
          ...row.list[newIndex],
          options: {
            ...row.list[newIndex].options,
            options: row.list[newIndex].options.options.map(item => ({
              ...item
            }))
          }
        })
      } else if (row.list[newIndex].type === 'button') {
        // console.log('before', row.list)
        this.$set(row.list, newIndex, {
          ...row.list[newIndex],
          key: Date.parse(new Date()) + '_' + Math.ceil(Math.random() * 99999),
          options: {
            ...row.list[newIndex].options
          },
          buttons: row.list[newIndex].buttons.map(item => ({
            ...item,
            id:
              'btn_' +
              Date.parse(new Date()) / 1000 +
              '' +
              Math.ceil(Math.random() * 99999)
          }))
        })
        // console.log('after', this.data.list[newIndex])
      }
      if (
        row.list[newIndex].options.labelSpan !== undefined &&
        row.list[newIndex].options.labelSpan === '2' &&
        row.list[newIndex].options.ctrlSpan === '2'
      ) {
        const containerwidthstr = window.getComputedStyle($event.srcElement)
          .width
        const containerwidth = parseInt(containerwidthstr.replace('px', ''))
        if (containerwidth < 400) {
          row.list[newIndex].options.labelSpan = '6'
          row.list[newIndex].options.ctrlSpan = '6'
        }
        if (
          (400 < containerwidth && containerwidth < 700) ||
          containerwidth === 400
        ) {
          row.list[newIndex].options.labelSpan = '2'
          row.list[newIndex].options.ctrlSpan = '4'
        }
      }
      //  this.selectWidget = row.list[newIndex]
      this.widgetFormSelect = row.list[newIndex]
      // console.log('handleWidgetColAdd end', row.list[newIndex].type)
    },
    showcode(funname) {
      if (this.showcodeeditor === true) {
        this.showcodeeditor = false
        return
      }
      const buildObject = window.frames.buildpublish.buildpubish(
        this.fromInfo,
        this.getjscode()
      )
      if (buildObject === false) {
        this.$message.error(`编辑源码脚本有错误！请检查！`)
        this.showcodeeditor = true
        return
      }

      this.formData.html = beautifier.html(buildObject.htmlcode)
      this.formData.js = beautifier.js(buildObject.jscode)
      this.formData.systemcss = buildObject.csscode
      this.showcodeeditor
        ? (this.showcodeeditor = false)
        : (this.showcodeeditor = true)
      if (this.showcodeeditor) {
        this.$refs.codeeditor.loadInit(funname)
      }
    },
    runCode() {
      const jsCodeStr = this.formData.js
      try {
        const postData = {
          type: 'refreshFrame',
          data: {
            generateConf: {
              fileName: '测试',
              type: 'file'
            },
            html: this.formData.html,
            js: jsCodeStr.replace('export default', ''),
            css: this.formData.css + this.formData.systemcss,
            scripts: [],
            links: []
          }
        }
        this.postData = postData
      } catch (err) {
        this.$message.error(`js错误：${err}`)
        console.error(err)
      }
    },
    preview() {
      const buildObject = window.frames.buildpublish.buildpubish(
        this.fromInfo,
        this.getjscode()
      )
      if (buildObject === false) {
        this.$message.error(`编辑源码脚本有错误！请检查！`)
        this.showcodeeditor = true
        return
      }
      this.formData.html = beautifier.html(buildObject.htmlcode)
      this.formData.js = beautifier.js(buildObject.jscode)
      this.formData.systemcss = buildObject.csscode
      //  this.$refs.apiconfiginfodialog.visible = true
      this.runCode()
    },
    getfromContext() {
      const buildObject = window.frames.buildpublish.buildpubish(
        this.fromInfo,
        this.getjscode()
      )
      if (buildObject === false) {
        this.$message.error(`编辑源码脚本有错误！请检查！`)
        this.showcodeeditor = true
        return
      }
      this.formData.html = buildObject.htmlcode
      this.formData.js = buildObject.jscode
      this.formData.systemcss = buildObject.csscode
      let formvuecode = this.formData.html
      formvuecode =
        formvuecode + '<' + 'script>' + this.formData.js + '</' + 'script>'
      formvuecode =
        formvuecode +
        '<' +
        'style scoped>' +
        (this.formData.css === undefined ? '' : this.formData.css) +
        this.formData.systemcss
      ;('</style>')
      return formvuecode
    },
    exportVue() {
      const formvuecode = beautifier.html(this.getfromContext())
      const blob = new Blob([formvuecode], { type: '' })
      const filename =
        this.fromInfo.formName !== '' ? this.fromInfo.formName : 'from'
      // const textStr = 'aaaaa,bbbbbbb,cccccc'
      FileSaver.saveAs(blob, filename + '.vue')
    },
    loadAfter(monacoEditor) {
      monacoEditor.setValue(beautifier.js(this.fromJson))
    },
    clickdesignType(typestr) {
      this.showcodeeditor = false
      this.fromInfo.deviceDesignType = typestr
    },
    addfrom(fromtype) {
      this.editcontroltype = 0
      this.formdesigndialog = true
      this.showcodeeditor = false
      this.formData = { html: '', js: '', css: '' }
      this.fromInfo = JSON.parse(JSON.stringify(defaultfromInfo))
      switch (fromtype) {
        case '1':
          this.fromInfo.designType = '/buildpublish.html'
          break
        case '2':
          this.fromInfo.designType = '/buildpublishAntDesign.html'
          break
        case '3':
          this.fromInfo.designType = '/buildquestionnaire.html'
          break
        case '4':
          this.fromInfo.designType = '/buildscreenanalyse.html'
          this.fromInfo.isadapt = false
          this.fromInfo.backgroundurl = '/images/bj.png'
          break
        case '5':
          this.fromInfo.designType = '/buildFlowFrom.html'
          break
      }
      this.fromInfo.fromTableName =
        'question' +
        Date.parse(new Date()) +
        '_' +
        Math.ceil(Math.random() * 99999)
      if (this.$refs.codeeditor !== undefined) {
        this.$refs.codeeditor.setValue('js', '')
        this.$refs.codeeditor.setValue('css', '')
      }
    },
    editfrom(FID) {
      this.editcontroltype = 0
      this.showcodeeditor = false
      this.formdesigndialog = true
      this.loadfromInfo(FID)
    },
    liulanfrom(FID) {
      apiComm('tappFrom.getFromContent', { fid: FID }).then(res => {
        const fromcontent = JSON.parse(res.data[0].fromcontent)
        this.formData.js = fromcontent.js
        this.formData.css = fromcontent.css
        this.formData.html = fromcontent.html
        this.$refs.apiconfiginfodialog.visible = true
        this.runCode()
      })
    },
    previewfun(editcontroltype, previewtype) {
      this.showcodeeditor = false
      if (this.fromInfo.designType === '/buildscreenanalyse.html') {
        this.preview()
        sessionStorage.setItem(
          'fromdesignViewData',
          JSON.stringify(this.postData)
        )
        window.open('/openviewfrom')
      } else {
        if (previewtype !== undefined && previewtype !== null) {
          this.previewType = previewtype
        }
        this.editcontroltype = editcontroltype
        if (
          this.$refs.PhoneSurvey !== null &&
          this.$refs.PhoneSurvey !== undefined
        ) {
          this.preview()
          this.$refs.PhoneSurvey.contentWindow.postMessage(
            this.postData,
            location.origin
          )
        }
        if (this.$refs.PcSurvey !== null && this.$refs.PcSurvey !== undefined) {
          this.preview()
          this.$refs.PcSurvey.contentWindow.postMessage(
            this.postData,
            location.origin
          )
        }
      }
    },
    PhoneSurveyiframeLoad() {
      if (
        this.$refs.PhoneSurvey !== null &&
        this.$refs.PhoneSurvey !== undefined
      ) {
        this.preview()
        this.$refs.PhoneSurvey.contentWindow.postMessage(
          this.postData,
          location.origin
        )
      }
    },
    PcSurveyiframeLoad() {
      if (this.$refs.PcSurvey !== null && this.$refs.PcSurvey !== undefined) {
        this.preview()
        this.$refs.PcSurvey.contentWindow.postMessage(
          this.postData,
          location.origin
        )
      }
    },
    dialogbutonclick(but) {
      this.selectWidget = but
      this.widgetFormSelect = but
    },
    handleBuildBtnEvent(cotorlEnent) {
      if (cotorlEnent.eventlist === undefined) {
        cotorlEnent.eventlist = []
      }
      if (cotorlEventConfig[cotorlEnent.type] !== undefined) {
        const controleventlist = cotorlEventConfig[cotorlEnent.type]
        if (controleventlist.length > 0) {
          const eventlist = cotorlEnent.eventlist.filter(item => {
            return item.label === controleventlist[0].label
          })
          if (eventlist === undefined || eventlist.length === 0) {
            cotorlEnent.eventlist.push({
              label: controleventlist[0].label,
              value: controleventlist[0].getfunction(cotorlEnent),
              content: controleventlist[0].content
            })
            this.showcode(controleventlist[0].getfunction(cotorlEnent))
          }
          if (eventlist.length > 0) {
            this.showcode(eventlist[0].value)
          }
        }
      }
    }
  },
  watch: {
    editcontroltype: {
      handler(val) {
        console.log(val)
      },
      deep: true
    }
  },
  created() {
    this.fromInfo = JSON.parse(JSON.stringify(defaultfromInfo))
  },
  mounted() {
    const h =
      document.documentElement.clientHeight || document.body.clientHeight
    this.codeh = h - 60
    loadBeautifier(btf => {
      beautifier = btf
    })
  },
  provide() {
    return {
      changeFromInfo: () => this.fromInfo,
      setFromInfo: val => (this.fromInfo = val),
      getEditControlType: () => this.editcontroltype,
      setEditControlType: val => (this.editcontroltype = val),
      setSelectWidget: val => {
        this.selectWidget = val
        this.widgetFormSelect = val
      }
    }
  }
}
</script>
<style>
.ant-card-body {
  padding-top: 5px;
  padding-right: 5px;
  padding-left: 0px;
  padding-bottom: 10px;
  height: calc(100% - 100px);
  overflow-y: auto;
}
</style>
<style scoped>
.el-button-group {
  float: right;
  padding-top: 15px;
  padding-right: 10px;
}
.el-button-group .el-button {
  padding: 12px 15px;
}
.el-icon-delete {
  color: red;
}
.el-icon-close {
  color: red;
}
.designType {
  width: 20px;
  height: 20px;
  margin-left: 5px;
  margin-right: 5px;
}
.selectdesignType {
  width: 25px;
  height: 25px;
  color: #40a9ff;
}
.pc {
  width: 100%;
  margin: 0 auto;
}
.mobile {
  width: 375px;
  margin: 0 auto;
}
.pad {
  width: 770px;
  margin: 0 auto;
}
.hiddenoverflow {
  overflow: hidden;
}

.ant-tabs-tabpane {
  border-bottom: 1px solid #ccc;
}
.ant-tabs-bar {
  margin-bottom: 5px;
}
.el-radio-group {
  line-height: 32px;
}
.form-control,
.single-line {
  background-image: none;
  border: 1px solid #ccc;
  border-radius: 1px;
  color: inherit;
  display: block;
  padding: 6px 12px;
  -webkit-transition: border-color 0.15s ease-in-out 0s,
    box-shadow 0.15s ease-in-out 0s;
  transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;
  width: 100%;
  font-size: 12px;
}

.required:before {
  content: '*';
  color: #c80000;
  font-size: 12px;
  vertical-align: top;
  display: inline-block;
  width: 10px;
  height: 10px;
  position: absolute;
  top: 2px;
  margin-left: -9px;
}
.label-name.required:before {
  top: auto;
  margin-top: 2px;
}
.el-input__inner {
  height: 32px;
  line-height: 32px;
  background-color: #fff;
  background-image: none;
  color: inherit;
  display: block;
  -webkit-transition: border-color 0.15s ease-in-out 0s,
    box-shadow 0.15s ease-in-out 0s;
  -webkit-transition: border-color 0.15s ease-in-out 0s,
    -webkit-box-shadow 0.15s ease-in-out 0s;
  transition: border-color 0.15s ease-in-out 0s,
    -webkit-box-shadow 0.15s ease-in-out 0s;
  transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;
  transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s,
    -webkit-box-shadow 0.15s ease-in-out 0s;
  width: 100%;
  font-size: 12px;
  border-radius: 0;
}
.el-input__suffix {
  right: 0px;
  line-height: 32px;
}

.el-switch__label * {
  font-size: 12px;
}
.border-title {
  line-height: 42px;
  padding-left: 15px;
  border-top: none;
  border-right: none;
}

.border-content {
  border-top: none;
  border-right: none;
  padding-right: 15px;
  padding-left: 15px;
  padding-top: 4px;
  padding-bottom: 4px;
}

.border-mult {
  background-color: #f5f5f5;
}

.border-mult .border-title {
  height: auto;
  border-bottom: none;
  border-left: none;
  margin-left: -2px;
}
</style>
<style scoped>
.el-card.box-card {
  height: 490px;
  top: 0px;
  position: absolute;
  width: 808px;
  z-index: 100;
  top: 58px;
}
.el-card.box-card .el-card__header {
  margin: 0;
  height: 46px;
  font-size: 18px;
  font-weight: 400;
  background-color: #f5f5f5;
  color: #409eff !important;
  border-bottom: 1px solid #ddd;
  text-align: center;
}
.el-card__header span {
  font-size: 15px;
  line-height: 10px;
}
.card-bottom {
  height: 40px;
  position: absolute;
  bottom: 0px;
  width: 100%;
  left: 0px;
  line-height: 40px;
  border-top: 1px solid #ccc;
  text-align: right;
  padding-right: 20px;
  z-index: 100;
  background-color: white;
}
.propertyborder-title {
  height: 42px;
  line-height: 42px;
  padding-left: 3px;
  border: 1px solid #ccc;
  border-top: none;
  border-right: none;
}

.propertyborder-content {
  border: 1px solid #ccc;
  border-top: none;
  border-right: none;
  padding-right: 3px;
  padding-left: 3px;
  padding-top: 4px;
  padding-bottom: 4px;
  height: 42px;
}

.components-list .tool-draggale {
  padding-left: 15px;
  text-align: center;
}
.top_logo {
  margin-top: -10px;
}
.top_ioc {
  cursor: pointer;
  margin-left: 10px;
  margin-top: 10px;
}
.top_logo button {
  margin-left: 5px;
}
.top_title {
  font-size: 14px;
  font-weight: bold;
}
.top_logo .anticon-close {
  float: right;
  padding-top: 15px;
  padding-right: 5px;
  cursor: pointer;
}
.top_logo .anticon-close svg {
  color: red;
  font-size: 16px;
  font-weight: bold;
}

.ant-layout-header {
  background: white;
  height: 56px;

  padding: 0px;
}
.ant-layout-sider {
  background: white;
  height: calc(100vh - 60px);
  border: 1px solid #ebeef5;
}
.ant-layout {
  background: white;
}
.ant-layout-content {
  height: calc(100vh - 60px);
  border-top: 1px solid #ebeef5;
  border-bottom: 1px solid #ebeef5;
}
</style>
<style lang="less" scoped>
.widget-form-container {
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;

  .widget-form-list {
    height: 100%;
    padding-bottom: 50px;
    clear: both;
    overflow: hidden;
    //  border: 2px dashed rgb(238, 238, 238);
    .widget-view {
      padding: 5px 10px 18px;
      margin: 0;
      position: relative;
      border: none;
      // border-left: 5px solid transparent;
      // height:44px;
      &.is_req {
        .el-form-item__label::before {
          content: '*';
          color: #f56c6c;
          margin-right: 4px;
        }
      }

      &:after {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        display: block;
        z-index: 1001;
      }

      &:hover {
        background: #ecf5ff;
      }

      &.active {
        background: #d3e2f1 !important;
        div {
          background: #d3e2f1 !important;
        }

        span {
          background: #d3e2f1 !important;
        }
      }
    }

    .widget-action-delete {
      position: absolute;
      right: 20px;
      bottom: -35px;
      z-index: 1009;
    }

    .widget-action-clone {
      position: absolute;
      right: 70px;
      bottom: -35px;
      z-index: 1009;
    }
  }

  .widget-grid {
    //background: #F4F6FC;
    position: relative;
    border-left: 0px solid transparent;
    // padding: 5px;
    margin-bottom: 5px;
    border-bottom: 1px solid #ccc;
    /*border-top: 1px solid #ccc;*/
    border-left: 1px solid #ccc;

    &.active {
      background: #d3e2f1 !important;

      div {
        background: #d3e2f1 !important;
      }

      span {
        background: #d3e2f1 !important;
      }

      button {
        background: #d3e2f1 !important;
      }

      label {
        background: #d3e2f1 !important;
      }
    }
  }

  .widget-grid-container {
    &.ghost {
      &::after {
        background: #fff;
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        top: 0;
        display: block;
        z-index: 10;
        content: '';
      }
    }
  }

  input {
    height: 32px;
  }

  .ghost {
    background: #fff;
    border: 1px dashed #409eff;
    float: left;
    height: 32px;
    box-sizing: border-box;

    &::after {
      background: #fff;
    }
  }

  .ghostpanel {
    background: #fff;
    border: 1px dashed #409eff;
    height: 100%;
    box-sizing: border-box;

    &::after {
      background: #fff;
    }
  }

  li.ghostpanel {
    height: 30px;
    list-style: none;
    font-size: 0;
  }

  li.ghost {
    height: 30px;
    list-style: none;
    font-size: 0;
  }
}
</style>
<style scoped>
.inputform div:first-child {
  width: 80px;
  display: inline-block;
  height: 40px;
  margin-top: 10px;
  margin-left: 10px;
}
.inputform .el-input {
  width: calc(100% - 100px);
  float: right;
}
.inputform .el-select {
  width: calc(100% - 100px);
  float: right;
}
.inputform button {
  margin-left: 5px;
  margin-right: 5x;
}
</style>
<style scoped>
.navbar {
  text-align: center;
  font-size: 0;
}
.navbar .icon_wj.active {
  color: #2672ff;
}
.navbar .icon_wj:first-child {
  border-right: 1px solid #eaeaea;
}
.navbar .icon_wj {
  display: inline-block;
  width: 74px;
  height: 39px;
  line-height: 39px;
  background: #fff;
  box-shadow: 0 2px 4px 0 rgb(0 0 0 / 5%);
  font-size: 12px;
  color: #aaa;
  box-sizing: border-box;
  cursor: pointer;
}
.navbar .icon_wj.active i.icon_mobile {
  background-position: -269px -171px;
}
.navbar .icon_wj i.icon_mobile {
  width: 14px;
  height: 19px;
  background-position: -293px -171px;
}
.navbar .icon_wj i {
  display: inline-block;
  background-image: url(https://s1.wenjuan.com/static/images/icon_edit_tools.png) !important;
  background-size: 520px auto;
  vertical-align: middle;
  margin-right: 6px;
}
.navbar .icon_wj {
  display: inline-block;
  width: 74px;
  height: 39px;
  line-height: 39px;
  background: #fff;
  box-shadow: 0 2px 4px 0 rgb(0 0 0 / 5%);
  font-size: 12px;
  color: #aaa;
  box-sizing: border-box;
  cursor: pointer;
}
.navbar .icon_wj i.icon_computer {
  width: 20px;
  height: 18px;
  background-position: -244px -171px;
}
.navbar .icon_wj i {
  display: inline-block;
  background-image: url(https://s1.wenjuan.com/static/images/icon_edit_tools.png) !important;
  background-size: 520px auto;
  vertical-align: middle;
  margin-right: 6px;
}
.preview_iframe {
  text-align: center;
}
.preview_iframe .phone {
  position: relative;
  margin-top: 10px;
  display: inline-block;
  width: 372px;
  height: 744px;
  background-image: url(https://s1.wenjuan.com/static/images/Exterior_style/appearset_v3/appearset_bgc_big.png);
  background-size: 372px 744px;
}

.preview_iframe .phone iframe[name='PhoneSurvey'] {
  width: 318px;
  height: 568px;
  margin: 74px 0 0;
}
.preview_iframe .hide {
  display: none !important;
}
.preview_iframe .computer iframe[name='PcSurvey'] {
  width: 98%;
  min-width: 1100px;
  min-height: 400px;
  margin: 0;
}
.fromtitle {
  margin-top: 20px;
  margin-bottom: 10px;
  padding-left: 10px;
  text-align: left;
  color: #484848;
  font-size: 20px;
}
.fromtitle input {
  border: 0px solid #dcdfe6;
  font-size: 20px;
}
.form_desc_content {
  margin-top: 20px;
  margin-bottom: 10px;
  padding-left: 10px;
}
.form_desc_content input {
  border: 0px solid #dcdfe6;
}
.tabright {
  height: calc(100vh - 128px);
  overflow-y: auto;
  overflow-x: hidden;
}
</style>
